package com.miyu.plugins.result;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 数据返回合集
 * 统一封装数据 统一做返回数据
 * 1 success 成功 2 fail 失败  3 error 错误 4 info 信息 5 warning 警告 类型
 * 正常类返回数据 其余不返还数据data
 * @author MIYU
 */

//@RestControllerAdvice
//@RequiredArgsConstructor
//public class ResultUtils implements ResponseBodyAdvice<Object> {
//
//
//    private final ObjectMapper objectMapper;
//
//    @Override
//    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
//
//        return true;
//    }
//
//    @Override
//    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//
//
//        if(body instanceof ResultSuccess||body instanceof ResultInfo){
//            return body;
//        }else
//         if(body instanceof String){
//                /* String属于特殊情况，需要单独处理，否则会报错*/
//                try {
//                    return objectMapper.writeValueAsString(ResultSend.success(body));
//                } catch (JsonProcessingException e) {
//                    ResultSend.exception(e.getMessage());
//                }
//            }
//            return ResultSend.success(body);
//        }
//}
